# -*- coding=utf-8 -*-

#目的是把50个数字，进行分组，每个组里面有不超过9个数字。
#类似每个网页的请求只能连续不超过10个
#使用线程的方式把数字打印出来，查看打印时间

import time
import math
from multiprocessing import Pool

'''
参考2：简书：https://www.jianshu.com/p/5ead77d5e96e
参考1：http://zmister.com/archives/90.html
'''


start_page_num=1
end_page_num=100
list_len=9


def print_list_content(listname,thread_name):
    for i in listname:
        print("thread"+thread_name+" is "+i)

def print_cell_content(cell):
    print(cell)

def group_count(groups,count):
    groups = groups

    values = [i for i in range(1, count+1)]
    values.sort(reverse=True)

    load_balance_groups = [[] for grp in range(groups)]

    for v in values:
        load_balance_groups.sort(key=lambda x: sum(x))
        load_balance_groups[0].append(v)
    return load_balance_groups

if __name__ == '__main__':
    start_time=time.time()
    begin_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
    print("开始时间是："+begin_time)

    #每组有9个，看能分成几个组
    groups=math.ceil(end_page_num/list_len)

    #查看每个分组里面有多少个数
    grouplist=group_count(groups,end_page_num)

    # for group in grouplist:
    #     pool = Pool(processes=4)
    #     pool.map_async(print_cell_content, group)
    #     pool.close()
    #     pool.join()
    #     print("该组已经执行完毕")

    pool = Pool(processes=4)
    for group in grouplist:
        pool.map_async(print_cell_content, group)
    pool.close()
    pool.join()
    print("共计耗时" + str(time.time() - start_time) + 's')